<template>
  <div>
    <el-table
            :data="tableData"
            stripe
            style="width: 100%">
      <el-table-column
              prop="pic"
              label="图片"
              width="100">
        <template slot-scope="scope">
          <el-image :src="scope.row.pic"></el-image>
        </template>
      </el-table-column>
      <el-table-column
              prop="name"
              label="商品名"
              width="180">
      </el-table-column>
      <el-table-column
              prop="price"
              label="价格"
              width="180">
      </el-table-column>
      <el-table-column
              prop="updateTime"
              label="上架日期">
      </el-table-column>
      <el-table-column
              fixed="right"
              label="操作"
              width="100">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">修改</el-button>
          <el-button @click="goodDel(scope.row)" type="text" size="small">下架</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  export default {
    name: "GoodList",
    data(){
      return {
        tableData: []
      }
    },
    methods:{
      handleClick(row) {
        this.$router.push({
          name: 'GoodAdd',
          params: {
            row
          }
        })
      },
      goodDel(row){
        // console.log(row);
        this.$axios.post("/soldOut",{
          id: row.id
        }).then(res=>{
          if(res.data.code == 200){
            for (let i = 0; i<this.tableData.length; i++) {
              if(this.tableData[i].id == row.id){
                this.tableData.splice(i,1);
                break;
              }
            }
            this.$message.success(res.data.message);
          }
        }).catch(err=>{
          console.log(err);
        })
      }
    },
    mounted() {
      this.$axios.post("/goodList",{
        userId: this.$store.state.userId
      }).then(res=>{
        for (let t of res.data) {
          t.updateTime = this.$moment(t.updateTime).format('YYYY-MM-DD HH:mm:ss');
        }
        this.tableData = res.data;
      }).catch(err=>{
        console.log(err);
      })
    }
  }
</script>

<style scoped>

</style>